package com.atguigu.gulimall.product.web;

import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.vo.Catelog2Vo;
import org.redisson.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
public class IndexController {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private RedissonClient redissonClient;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping({"/","/index.html","index"})
    public String indexPage(Model model){
        List<CategoryEntity> categoryEntities=categoryService.getLevel1Category();
        model.addAttribute("categorys",categoryEntities);
        return "index";
    }

    @ResponseBody
    @GetMapping("/index/json/catalog.json")
    public Map<String, List<Catelog2Vo>> getCatelog(){
        Map<String, List<Catelog2Vo>> map=categoryService.getCatelog();
        return map;
    }

//    测试分布式锁
    @GetMapping("/write")
    @ResponseBody
    public String write(){
        String s = UUID.randomUUID().toString();
        RReadWriteLock readWriteLock = redissonClient.getReadWriteLock("rw-lock");
        RLock rLock = readWriteLock.writeLock();
        rLock.lock();
        try {
            stringRedisTemplate.opsForValue().set("rwString",s);
            Thread.sleep(20000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            rLock.unlock();
        }
        return s;
    }

//    测试分布式锁
    @GetMapping("/read")
    @ResponseBody
    public String read(){
        RReadWriteLock readWriteLock = redissonClient.getReadWriteLock("rw-lock");
        RLock rLock = readWriteLock.readLock();
//        RLock rLock = redissonClient.getLock("r-lock");
        String rwString="123";
        try {
            rLock.lock();
//            rLock.lock(10, TimeUnit.SECONDS);
            rwString= stringRedisTemplate.opsForValue().get("rwString");
            Thread.sleep(10000);
        }catch (Exception e){
            System.out.println(e);
        }finally {

            rLock.unlock();
        }
        return rwString;
    }
    //信号量锁
    @GetMapping("/park")
    @ResponseBody
    public String park() throws InterruptedException {
        RSemaphore car = redissonClient.getSemaphore("car");
//        car.acquire();
        boolean b = car.tryAcquire();
        return "park";
    }

    @GetMapping("/go")
    @ResponseBody
    public String go() throws InterruptedException {
        RSemaphore car = redissonClient.getSemaphore("car");
        car.release();
        return "ok";
    }

    @GetMapping("/down")
    @ResponseBody
    public String down() throws InterruptedException {
        RCountDownLatch door = redissonClient.getCountDownLatch("door");
        door.trySetCount(5);
        door.await();
        return "关门了";
    }

    @GetMapping("/gogo/{id}")
    @ResponseBody
    public String gogo(@PathVariable("id")Integer id){
        RCountDownLatch door = redissonClient.getCountDownLatch("door");
        door.countDown();
        return id+"班放假了";

    }
}
